/*
 * Copyright 2013 Nikolay Todorov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jobinsight.util;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;



/**
 * Prints an object to JSON.
 * 
 * @author todorov
 *
 */
public class Printer2 {
	DateFormat  dateFormatter = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM,SimpleDateFormat.SHORT);
	Set IGNORE_FIELDS = toSet("serialVersionUID",
			// PrimaryObject deceases
			"sourceToTargetClassesHT","sourceToTargetFieldsHT","sourceToTargetConstructorsHT"
			);
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ObjReader reader = new ObjReader();
					Object objToDisplay = reader.getObject();
					File objPath = reader.getFile();
					
					if(objToDisplay!=null)
						start( objPath, objToDisplay );
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void writeData(File objfile, Object objToDisplay) {
		Gson g = new GsonBuilder().setPrettyPrinting().create();
		Writer writer;
		try {
			writer = new FileWriter(new File(objfile+".json"));
			g.toJson(objToDisplay, writer);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	public static void start(File  objfile, Object objToDisplay){
		try{
			Printer2 window = new Printer2();
			window.writeData(objfile, objToDisplay);
		}catch( Exception e ){
			e.printStackTrace();
		}
	}

	public static Set<String> toSet(String... values) {
		return new HashSet<String>(java.util.Arrays.asList(values));
	}

}